package com.fengba.nanzhi.myfengba;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 89311 on 2018/4/11.
 */

public class MyCFWAdapter extends RecyclerView.Adapter<MyCFWAdapter.LinerViewHolder>{
    private Context context;
    private JSONArray jsonArray;
    private MyCF notice;
    private JSONObject job;
    //商品价格
    String order_name;
    //商品价格
    double order_money;
    //疯豆数量
    int credit;
    //图片
    String images;
    //发货状态(0待发货,1已发货)',
    int delivery;
    String[]delivery_ = new String[]{"待发货 ","已发货 "};
    //订单状态0待支付,1已付款,2 已取消,3未开奖,4未中奖(预付款退回中),5未中奖(预付款已退回)',
    int status;
    String[]status_ = new String[]{"待支付 ","已付款 ","已取消 ","未开奖 ","未中奖(预付款退回中) ","未中奖(预付款已退回) "};
    int id;
    //运费
    int freight = 0;
    //购买数量
    int buy_number;


    public MyCFWAdapter(Context context, MyCF add) {
        this.context = context;
        this.notice = add;
    }

    @Override
    public MyCFWAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_my_item_view,parent,false));
    }


    @Override
    public void onBindViewHolder(MyCFWAdapter.LinerViewHolder holder, final int position) {
        try {
            holder.Miv03.getLayoutParams().width = 0;
            holder.Miv02.getLayoutParams().width = 0;
            job = (JSONObject) jsonArray.get(position);
            holder.Mtv10.setWidth(0);
            holder.Mtv05.setWidth(0);
            order_name= job.get("order_name").toString();
            holder.Mtv01.setText(order_name);
            order_money=Double.parseDouble(job.get("order_money").toString());
            holder.Mtv04.setText("￥"+order_money+"");
            credit =Integer.parseInt(job.get("credit").toString());
            holder.Mtv05.setText(String.valueOf(credit));
            Glide.with(context).load(job.get("images").toString().split(",")[0]).into(holder.MIv01);
            status = Integer.parseInt(job.get("status").toString());
            //订单状态0待支付,1已付款,2 已取消,3未开奖,4未中奖(预付款退回中),5未中奖(预付款已退回)',
            holder.Mtv06.setText(status_[status].toString());
            if (status==1){
             delivery =Integer.parseInt(job.get("delivery").toString());
             //发货状态(0待发货,1已发货)',
             holder.Mtv06.setText(delivery_[delivery].toString());
            }
            id =(int)job.get("id");
            buy_number =  Integer.parseInt(job.get("buy_number").toString());
            holder.Mtv07.setText("x"+String.valueOf(buy_number));
            holder.Mtv09.setText((order_money*buy_number)+"");
            holder.Mtv10.setText((credit*buy_number)+"");

            holder.Nll01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context,OrderDetailed.class);
                    try {
                        intent.putExtra("id",((JSONObject) jsonArray.get(position)).get("id").toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    context.startActivity(intent);
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        jsonArray = notice.getJsonArrays();
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
            {
                return 0;
            }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private ImageView MIv01;
        private ImageView Miv03;
        private TextView Mtv01;
        private ImageView Miv02;
        private TextView Mtv04;
        private TextView Mtv05;
        private TextView Mtv06;
        private TextView Mtv07;
        private TextView Mtv09;
        private TextView Mtv10;
        private RelativeLayout Nll01;
        public LinerViewHolder(View itemView) {
            super(itemView);
            MIv01 = itemView.findViewById(R.id.MIv01);
            Miv03 = itemView.findViewById(R.id.Miv03);
            Mtv01 = itemView.findViewById(R.id.Mtv01);
            Mtv04 = itemView.findViewById(R.id.Mtv04);
            Mtv05 = itemView.findViewById(R.id.Mtv05);
            Mtv07 = itemView.findViewById(R.id.Mtv07);
            Mtv06 = itemView.findViewById(R.id.Mtv06);
            Mtv09 = itemView.findViewById(R.id.Mtv09);
            Mtv10 = itemView.findViewById(R.id.Mtv10);
            Nll01 = itemView.findViewById(R.id.Nll01);
            Miv02 = itemView.findViewById(R.id.Miv02);
        }
    }


}
